﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using System.Collections.ObjectModel;
using KOF.Helpers;

namespace KOF.Views
{
    public partial class Refine : PhoneApplicationPage
    {
      
        public Refine()
        {
            InitializeComponent();
        }

        // Set Past if status =0 and upcoming if status =1
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                if ((App.Current as App).ReleaseStatusTemp == 0)
                {
                    btnRelease2.Content = "Past";
                }
                else
                {
                    btnRelease2.Content = "Upcoming";
                }

               
                if ((App.Current as App).gSelectedBrands != null && (App.Current as App).gSelectedBrands.Count > 0)
                {
                    
                    btnBrands2.Content = "";
                    string strContent = string.Empty;

                    var checkAll = (from b in (App.Current as App).gSelectedBrands
                                    where b.id == "0"
                                    select b).FirstOrDefault();

                    // Set All if id 0 exist
                    if (checkAll != null)
                    {
                        btnBrands2.Content = "All";
                    }
                        // show brands on button saperated by comma
                    else
                    {
                        foreach (KOF.Helpers.ResponseBrand brand in (App.Current as App).gSelectedBrands)
                        {
                            strContent += brand.brand_name + ", ";
                        }

                        if (!String.IsNullOrWhiteSpace(strContent) && strContent.Trim().EndsWith(","))
                        {
                            strContent = strContent.Remove(strContent.LastIndexOf(",")).Trim();
                        }

                        if (!String.IsNullOrWhiteSpace(strContent) && strContent.Length > 19)
                        {
                            btnBrands2.Content = strContent.Substring(0, 17) + "...";
                        }
                        else
                        {
                            btnBrands2.Content = strContent;
                        }
                    }
                }
                else
                {
                    btnBrands2.Content = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            base.OnNavigatedTo(e);
        }
        
        // Redirect to ReleaseStatus Tab - Button Click
        private void btnRelease_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.NavigationService.Navigate(new Uri("/Views/ReleaseStatus.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        // Redirect to Brands Tab - Button click
        private void btnBrands_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.NavigationService.Navigate(new Uri("/Views/Brands.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        // Redirect to Brands Tab - Image Click
        private void imgBrands_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                this.NavigationService.Navigate(new Uri("/Views/Brands.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        // Redirect to ReleaseStatus Tab - image Click
        private void imgRelease_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                this.NavigationService.Navigate(new Uri("/Views/ReleaseStatus.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        // On Cancel Button Save Selected Brands and Release Status( Upcoming or Past) and Redirect to Release Tab
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
 
            try
            {
                ObservableCollection<Helpers.ResponseBrand> lstTempBrands = new ObservableCollection<Helpers.ResponseBrand>();
                foreach (ResponseBrand brand in (App.Current as App).gSelectedBrands)
                {
                    KOF.Helpers.ResponseBrand newRec = new Helpers.ResponseBrand();
                    newRec.id = brand.id;
                    newRec.brand_name = brand.brand_name;
                    newRec.subtypes = brand.subtypes;
                    if (brand.subtypes != null && brand.subtypes.Count > 0)
                    {
                        foreach (ResponseBrand sub in brand.subtypes)
                        {
                            if (sub.checked1) lstTempBrands.Add(sub);
                        }
                    }
                    lstTempBrands.Add(newRec);
                }
              
                IsolatedStorageSettings.ApplicationSettings.Remove("SelectedBrands");
                IsolatedStorageSettings.ApplicationSettings["SelectedBrands"] = lstTempBrands;
                IsolatedStorageSettings.ApplicationSettings.Save();

                (App.Current as App).ReleaseStatus = (App.Current as App).ReleaseStatusTemp;
                IsolatedStorageSettings.ApplicationSettings["ReleaseStatus"] = (App.Current as App).ReleaseStatus;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            this.NavigationService.GoBack();
        }

        // Save Selected Brands and Release Status and Redirect to Release Tab
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ObservableCollection<Helpers.ResponseBrand> lstTempBrands = new ObservableCollection<Helpers.ResponseBrand>();
                foreach (ResponseBrand brand in (App.Current as App).gSelectedBrands)
                {
                    KOF.Helpers.ResponseBrand newRec = new Helpers.ResponseBrand();
                    newRec.id = brand.id;
                    newRec.brand_name = brand.brand_name;
                    newRec.subtypes = brand.subtypes;
                    if (brand.subtypes != null && brand.subtypes.Count > 0)
                    {
                        foreach (ResponseBrand sub in brand.subtypes)
                        {
                            if (sub.checked1) lstTempBrands.Add(sub);
                        }
                    }
                    lstTempBrands.Add(newRec);
                }
                
                IsolatedStorageSettings.ApplicationSettings.Remove("SelectedBrands");
                IsolatedStorageSettings.ApplicationSettings["SelectedBrands"] = lstTempBrands;
                IsolatedStorageSettings.ApplicationSettings.Save();

                (App.Current as App).ReleaseStatus = (App.Current as App).ReleaseStatusTemp;
                IsolatedStorageSettings.ApplicationSettings["ReleaseStatus"] = (App.Current as App).ReleaseStatus;
                IsolatedStorageSettings.ApplicationSettings.Save();
                if (KOF.App.ViewModel.IsDataLoaded)
                {
                    KOF.App.ViewModel.IsDataLoaded = false;
                }
               
                KOF.App.ViewModel.ListItemSource.Clear();

                IsolatedStorageSettings.ApplicationSettings["loader"] = true;
                this.NavigationService.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

    }
}